<template>
  <div class="app-container">
    <div class="btnBox">
      <p style="margin-left: 10px;">选择字典类型：</p>
      <div
        v-for="item in listType"
        :key="item.types"
        :class="[ item.id === typesId ? 'topBtnAc' : 'topBtnNo', 'topBtn']"
        @click="getList(item.id)"
      >{{item.types}}</div>

<!--      <cus-pagination-->
<!--        v-show="totalTop>0"-->
<!--        :total="totalTop"-->
<!--        :background=false-->
<!--        :page.sync="listQueryTop.page"-->
<!--        :limit.sync="listQueryTop.pageSize"-->
<!--        small layout="prev, pager, next"-->
<!--      ></cus-pagination>-->
    </div>
    <cus-wraper>
      <cus-filter-wraper>
        <div>
          <el-select filterable v-model="flagIndex" @change="reflag" class="filter-item" placeholder='请选择' style="width: 200px;">
            <el-option v-for="item in flagList" :key="item.key" :label="item.lab" :value="item.key"></el-option>
          </el-select>
          <el-input v-if="flagIndex !== '4'" v-model="searchVal" placeholder="请输入分组Id" style="width:200px;margin: 0 10px;" clearable></el-input>
          <el-select filterable v-else v-model="flagText2" class="filter-item" placeholder='请选择' style="width: 200px;margin: 0 10px;">
            <el-option v-for="item in flagList2" :key="item.key" :label="item.lab" :value="item.key"></el-option>
          </el-select>
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="info" @click="reGetList" icon="el-icon-search">重置</el-button>
        </div>
        <el-button  type="primary" @click="handleCreate" icon="el-icon-plus">添加</el-button>
      </cus-filter-wraper>
      <div class="table-container">
        <el-table v-loading="listLoading" :data="list" size="mini" fit element-loading-text="Loading"
                  highlight-current-row >
          <el-table-column label="ID" prop="id" align="center"></el-table-column>
          <el-table-column label="理由" prop="reasonDetail" align="center"></el-table-column>
          <el-table-column label="状态" prop="isDisable" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="success" v-if="scope.row.isDisable === 0" round>已启用</el-button>
              <el-button size="mini" type="danger" v-if="scope.row.isDisable === 1" round>已禁用</el-button>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdTime" align="center"></el-table-column>

          <el-table-column  align="right" v-if="this.global_checkBtnPermission(['sys:user:edit','sys:user:delete'])" label="操作">
            <template slot-scope="scope">
              <el-button  size="mini" type="primary" @click="handleUpdate(scope.row)" icon="el-icon-edit" plain>
                {{scope.row.isDisable === 0?'禁用':'启用'}}
              </el-button>
              <cus-del-btn @ok="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      </div>

      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogVisible" width="40%" v-dialogDrag @close="handleDialogClose">
        <el-form ref="dataForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="理由:" prop="reasonDetail">
            <el-input v-model="form.reasonDetail" ></el-input>
          </el-form-item>
          <el-form-item label="状态:" prop="groupOrder">
            <el-radio v-model="form.isDisable" label='0'>启用</el-radio>
            <el-radio v-model="form.isDisable" label='1'>禁用</el-radio>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false"> 取 消 </el-button>
          <el-button type="primary" @click="submitForm"> 确 认 </el-button>
        </span>
      </el-dialog>
    </cus-wraper>
  </div>
</template>

<script>
  import { getRoomPage, saveSysUser, deleteSysUser, updatereasonDetailDictionaries, getListType } from '@/api/dictionaries/reason'

  export default {
    data() {
      return {
        dialogVisible: false,
        list: [],
        listLoading: false,
        total: 0,
        listQuery: {
          page: 1,
          pageSize: 10,
          labelName: undefined
        },
        totalTop: 50,
        listQueryTop: {
          page: 1,
          pageSize: 10
        },
        input: '',
        form: {
          labelName: undefined, //主键ID
          isDisable:'0',
          typesId: undefined,
        },
        flagList: [{ key: '1', lab: 'id' },{ key: '3', lab: '理由' }, { key: '4', lab: '状态' }],
        flagList2: [{ key: '0', lab: '启用' }, { key: '1', lab: '禁用' } ],
        flagIndex:'1',
        flagText:'',
        flagText2:'',
        searchVal:'',
        dialogStatus: '',
        titleMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          labelName: [
            {required: true, message: '请输入名称', trigger: 'blur'},
          ]
        },
        listType:[],
        typesId: 1,
      }
    },
    created() {
      // this.getList()
      this.getListType()
    },
    methods: {
      getListType(){
        getListType().then(response => {
          // response)
          this.listType = response.data.records
          this.typesId = response.data.records[0].id
          this.getList(this.typesId);
        })
      },
      // getAllList()
      getList(id) {
        this.typesId = id
        this.listQuery.typesId = id
        getRoomPage(this.listQuery).then(response => {

          this.list = response.data.records
          this.total = response.data.total
        })
      },
      search(){
        this.listQuery.id = undefined
        this.listQuery.reasonDetail = undefined
        this.listQuery.isDisable = undefined

        if(this.flagIndex === '1'){
          this.listQuery.id = this.searchVal;
        }else if(this.flagIndex === '3'){
          this.listQuery.reasonDetail = this.searchVal;
        }else if(this.flagIndex === '4'){
          this.listQuery.isDisable = this.flagText2;
        }
        this.getList(this.typesId);
      },
      reGetList(){
        this.searchVal = ''
        this.listQuery.id = undefined
        this.listQuery.reasonDetail = undefined
        this.listQuery.isDisable = undefined
        this.getList(this.typesId);
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogVisible = true
      },
      handleUpdate(row) {

        let data = {
          "id": row.id,
          "reasonDetail": row.reasonDetail,
          "isDisable": row.isDisable === 0?1:0
        }

        saveSysUser(data).then(response => {
          if (response.code === 0) {
            this.getList(this.typesId)
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
            this.dialogVisible = false
          }
        }).catch(err => err)
      },
      handleDelete(row) {
        let id = { "id":row.id}
        deleteSysUser(id).then(response => {
          if (response.code === 0) {
            this.getList(this.typesId)
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },
      submitForm() {
        this.form.typesId = this.typesId
        // this.form)
        // return
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            saveSysUser(this.form).then(response => {
              if (response.code === 0) {
                this.getList(this.typesId)
                this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
                this.dialogVisible = false
              } else {
                this.$message({
                message: response.message,
                type: 'error'
            });
              }
            }).catch(err => {

            })
          }
        })
      },
      resetForm() {
        this.form = {
          id: undefined, //主键ID
          isDisable:'0',
        }
      },
      // 监听dialog关闭时的处理事件
      handleDialogClose() {
        if (this.$refs['dataForm']) {
          this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .search-line{
    margin-bottom: 10px;
    padding: 10px;
    background: white;
  }
  .filter-wraper{
    display: flex;
    justify-content: space-between;
  }
  .table-container /deep/ .el-table .cell{
    display:flex;
  }
  .app-container /deep/  .el-table{
    max-width: calc(100vw - 490px);
    width: 90vw;
  }
  .app-container{
    display:flex;
  }
  .btnBox{
    width: 200px;
    background: white;
    border-right: 1px solid #EBEEF5;
  }
  .leftBtn{
    margin:10px;
  }
  .topBtn{
    width: 180px;
    height: 36px;
    line-height: 36px;
    text-align: center;
    border-radius: 18px;
    margin: 10px auto;
    padding: 0 20px;
    font-size: 14px;
    cursor: pointer;
  }
  .topBtnNo{
    color: #409EFF;
    background: #ecf5ff;
    border: 1px solid #b3d8ff;
  }
  .topBtnAc{
    background: #3a8ee6;
    border: 1px solid #3a8ee6;
    color: #FFF;
  }
</style>
